/*
* @Author: Adolphus.Yao
* @Date:   2019-08-28 11:52:42
* @Last Modified by:   Adolphus.Yao
* @Last Modified time: 2019-08-28 12:11:37
*/
package counter

import (
    "time"
    "github.com/kataras/iris"
    "github.com/kataras/iris/core/host"
)

func Configurator(app *iris.Application) {
    counterValue := 0

    go func() {
        ticker := time.NewTicker(time.Second)
        for range ticker.C {
            counterValue++
        }
        app.ConfigureHost(func(h *host.Supervisor) {
            // 监督服务是否停止，停止就要停止定时器
            h.RegisterOnShutdown(func() {
                ticker.Stop()
            })
        })
    }()

    app.Get("/counter", func(ctx iris.Context) {
        ctx.Writef("Counter value = %d", counterValue)
    })
}